<!-- 驾驶员报账列表 -->
<template>
  <div class="driver-item">
    <div ref="dgtools" class="filter-container">
      <div class="dgts-row">
        <div class="dgts-text-tj1">
          <b class="tr-it-title c-green">累计</b>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-green">收入：{{ dg.totalGath }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">支出：{{ dg.totalPay }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">打单费：{{ dg.singleFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">洗车费：{{ dg.washingFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">停车费：{{ dg.parkingFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">过路费：{{ dg.roadFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">用餐费：{{ dg.livingFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">买水费：{{ dg.waterFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">住宿费：{{ dg.stayFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">其他费：{{ dg.otherFee }} 元</span>
        </div>
      </div>

      <div class="dgts-row">
        <div class="dgts-text-tj2">
          <b class="tr-it-title c-green">所选</b>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-green">收入：{{ seledGath }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">支出：{{ seledPay }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">打单费：{{ seledSingleFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">洗车费：{{ seledWashingFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">停车费：{{ seledParkingFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">过路费：{{ seledRoadFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">用餐费：{{ seledLivingFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">买水费：{{ seledWaterFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">住宿费：{{ seledStayFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">其他费：{{ seledOtherFee }} 元</span>
          <el-divider direction="vertical"></el-divider>
          <span class="tr-it c-red">效率值：{{ seledOtherFee1 }} %</span>
        </div>
      </div>

      <div class="dgts-fm">
        <el-select v-model="dg.ps.timeType" placeholder="时间类型" style="width: 100px">
          <el-option label="添加时间" value="1"></el-option>
          <el-option label="发生时间" value="2"></el-option>
        </el-select>

        <el-date-picker clearable v-model="dg.ps.sedate" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 210px" />

        <el-select v-model="dg.ps.isCheck" clearable multiple collapse-tags placeholder="报账状态" style="width: 140px">
          <el-option label="未审核" value="NO_CHECK"></el-option>
          <el-option label="已审核" value="CHECKED"></el-option>
          <el-option label="已报销" value="CANCELLATION"></el-option>
        </el-select>

        <!-- <el-select 
          v-model="dg.ps.driverPayType" 
          clearable 
          placeholder="驾驶员报账"
          style="width: 120px">
          <el-option 
            v-for="item in driverOptions" 
            :key="item.value" 
            :label="item.label" 
            :value="item.value">
          </el-option>
        </el-select> -->

        <el-select v-model="dg.ps.uname" clearable filterable placeholder="经办人" style="width: 100px">
          <el-option v-for="item in serviceManList" :key="item.baseUserId.uname" :label="item.baseUserId.realName"
            :value="item.baseUserId.uname">
          </el-option>
        </el-select>

        <el-select v-model="dg.ps.cusUname" clearable filterable remote reserve-keyword :remote-method="getDriverList"
          placeholder="收款人" style="width: 100px">
          <el-option v-for="item in cusUnameOptions" :key="item && item.uname" :label="item && item.realName"
            :value="item && item.uname" />
        </el-select>

        <el-input style="width: 100px" clearable placeholder="报账金额" v-model="dg.ps.money">
        </el-input>

        <el-select v-model="dg.ps.plateNum" clearable filterable placeholder="车牌号" style="width: 100px">
          <el-option v-for="item in carNumList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>

        <el-select v-model="dg.ps.reimTypeId" clearable multiple filterable collapse-tags placeholder="报账类型"
          style="width: 190px">
          <el-option v-for="item in reimTypeOptions" :key="item.id" :label="item.typeName" :value="item.id">
          </el-option>
        </el-select>

        <el-input style="width: 180px" clearable placeholder="操作标示" v-model="dg.ps.operMark">
        </el-input>

        <el-input style="width: 180px" clearable placeholder="子订单号" v-model="dg.ps.orderNum">
        </el-input>

        <el-input style="width: 180px" clearable placeholder="摘要/其他费用说明" v-model="dg.ps.remark">
        </el-input>

        <el-button type="primary" icon="el-icon-search" @click="getDgList">
          搜索
        </el-button>

        <div>
          <el-button type="primary" icon="el-icon-plus"
            @click="setAdupDriverStaffReimMd({ state: true, type: 1 })">新增</el-button>
          <el-button type="primary" icon="el-icon-edit"
            @click="setAdupDriverStaffReimMd({ state: true, type: 2 })">修改</el-button>
          <el-button type="primary" icon="el-icon-s-check" @click="auditStaffReim('CHECKED')">审核通过</el-button>
          <el-button type="primary" icon="el-icon-s-check" @click="auditStaffReim('REFUSED')">审核不通过</el-button>
          <el-button type="primary" icon="el-icon-s-release"
            @click="setTurnDownStaffReimMd({ state: true })">驳回</el-button>
          <el-button type="primary" icon="el-icon-document-copy"
            @click="setCancelStaffReimMd({ state: true })">核销</el-button>
          <el-button type="primary" icon="el-icon-document-close" @click="cancelWriteOff">撤销核销</el-button>
          <el-button type="primary" icon="el-icon-delete" @click="delStaffReim">删除</el-button>
          <el-button type="primary" icon="el-icon-download" @click="exportStaffReim">导出</el-button>
        </div>
      </div>
      <TableSelfSet v-if="dg.columns.length > 0" :tbTitle="'员工报账-驾驶员报账'" :dgName="dg.dgName" :tbId="dg.tbName"
        :srcCols="dg.srcCols" :columns="dg.columns" :showSeledInfo="this" @saveSet="saveTbSet" />
    </div>

    <el-table :ref="dg.tbName" :data="dg.list" row-key="id" :height="bodyHeight - 50" border fit
      :row-class-name="rowClassName" @header-dragend="dragHeader" @selection-change="selRowChange">
      <el-table-column fixed="left" type="index" label="编号" align="center" :resizable="false" width="40">
      </el-table-column>
      <el-table-column fixed="left" type="selection" align="center" :resizable="false" width="40">
      </el-table-column>
      <el-table-column v-for="(col, index) in dg.columns" :key="index" :class-name="dg.dgName" :prop="col.field"
        :label="col.label" :width="col.width">
        <template slot-scope="{row}">
          <template v-if="col.field == 'isCheck'">
            <b v-if="row.isCheck == 'NO_CHECK'" class="c-yellow">未审核</b>
            <b v-else-if="row.isCheck == 'CHECKED'" class="c-green">已审核</b>
            <b v-else-if="row.isCheck == 'CANCELLATION'" class="c-bbb">已报销</b>
            <b v-else class="c-red">未通过</b>
          </template>

          <template v-else-if="col.field == 'reimVoucherUrl'">
            <div class="flex" v-if="row.reimVoucherUrl">
              <template v-if="row.reimVoucherUrl.indexOf('=') != -1">
                <div v-for="img in row.reimVoucherUrl.split(',')" :key="img.split('=')[0]">
                  <el-image v-if="img" class="list-row-img" alt="" title="点击查看大图"
                    :src="fileBaseUrl + '/kcbFiles' + img.split('=')[1]"
                    :preview-src-list="[fileBaseUrl + '/kcbFiles' + img.split('=')[1]]">
                    <div slot="error">
                      <i class="el-icon-picture-outline fs-18"></i>
                    </div>
                  </el-image>
                </div>
              </template>
              <template v-else>
                <div v-for="img in row.reimVoucherUrl.split(',')" :key="img">
                  <el-image v-if="img" class="list-row-img" alt="" title="点击查看大图" :src="fileBaseUrl + img"
                    :preview-src-list="[fileBaseUrl + img]">
                    <div slot="error">
                      <i class="el-icon-picture-outline fs-18"></i>
                    </div>
                  </el-image>
                </div>
              </template>
            </div>
            <span v-else class="c-bbb">未设置</span>
          </template>

          <template v-else-if="col.field == 'orderNum'">
            <span v-if="row.carOrderReim">
              {{ row.carOrderReim.orderNum }}
            </span>
          </template>

          <template v-else-if="col.field == 'routeDetail'">
            <span v-if="row.carOrderReim">
              <span v-html="row.carOrderReim.routeDetail.replace(/@/g, '<br />')"></span>
            </span>
          </template>

          <template v-else-if="col.field == 'routeSdate'">
            <span v-if="row.carOrderReim">
              {{ row.carOrderReim.stime | time('yyyy年MM月dd号') }}
            </span>
          </template>

          <template v-else-if="col.field == 'routeEdate'">
            <span v-if="row.carOrderReim">
              {{ row.carOrderReim.etime | time('yyyy年MM月dd号') }}
            </span>
          </template>

          <template v-else-if="col.field == 'isDeep'">
            <span v-if="row.carOrderReim">
              {{ row.carOrderReim.routeLineInfo.isHighSpeed == 1 ? '走高速' : '不走高速' }}
            </span>
          </template>

          <template v-else-if="col.field == 'costStartDate'">
            <span v-if="row.reimTypeId.typeMark != 'YTCF' && row.reimTypeId.typeMark != 'YXCF'">
              {{ row.payDate | time('yyyy年MM月dd号') }}
            </span>
            <span v-else>{{ row.costStartDate | time('yyyy年MM月dd号') }}</span>
          </template>

          <template v-else-if="col.field == 'startTime'">
            <span v-if="row.reimTypeId.typeMark != 'YTCF' && row.reimTypeId.typeMark != 'YXCF'">
              {{ row.payDate | time('HH:mm') }}
            </span>
            <span v-else>{{ row.costStartDate | time('HH:mm') }}</span>
          </template>

          <template v-else-if="col.field == 'costEndDate'">
            {{ row.costEndDate | time('yyyy年MM月dd号') }}
          </template>

          <template v-else-if="col.field == 'endTime'">
            {{ row.costEndDate | time('HH:mm') }}
          </template>

          <template v-else-if="col.field == 'currentKilo'">
            <span v-if="row.driverJzRecordId">{{ row.driverJzRecordId.currentKilo }}公里</span>
          </template>

          <template v-else-if="col.field == 'singleFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.singleFee }}元</span>
          </template>

          <template v-else-if="col.field == 'washingFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.washingFee }}元</span>
          </template>

          <template v-else-if="col.field == 'parkingFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.parkingFee }}元</span>
          </template>

          <template v-else-if="col.field == 'roadFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.roadFee }}元</span>
          </template>

          <template v-else-if="col.field == 'livingFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.livingFee }}元</span>
          </template>

          <template v-else-if="col.field == 'waterFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.waterFee }}元</span>
          </template>

          <template v-else-if="col.field == 'stayFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.stayFee }}元</span>
          </template>

          <template v-else-if="col.field == 'otherFee'">
            <span v-if="row.orderTrade">{{ row.orderTrade.otherFee }}元</span>
          </template>

          <template v-else>{{ getColVal(row, col.field) }}</template>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="dg.total > 0" :total="dg.total" :page.sync="dg.ps.page" :limit.sync="dg.ps.rows"
      @pagination="getDgList" />

    <AdupDriverStaffReim v-if="adupDriverStaffReimMd.state" :myData="adupDriverStaffReimMd"
      @myClick="setAdupDriverStaffReimMd" @reloadDg="getDgList" />

    <AuditStaffReim v-if="auditStaffReimMd.state" :myData="auditStaffReimMd" @myClick="setAuditStaffReimMd"
      @reloadDg="getDgList" />

    <TurnDownStaffReim v-if="turnDownStaffReimMd.state" :myData="turnDownStaffReimMd" @myClick="setTurnDownStaffReimMd"
      @reloadDg="getDgList" />

    <CancelStaffReim v-if="cancelStaffReimMd.state" :myData="cancelStaffReimMd" @myClick="setCancelStaffReimMd"
      @reloadDg="getDgList" />
  </div>
</template>

<script>
import DataGridBase from "@/views/mixins/DataGridBase";
import DataGridResize from '@/views/mixins/DataGridResize';
import TableSelfSet from '@/components/TableSelfSet';
import Pagination from '@/components/Pagination';
import { exportExcel } from '@/utils/exportFile'

import AdupDriverStaffReim from './adup-driver-staff-reim';
import AuditStaffReim from './audit-staff-reim';
import TurnDownStaffReim from './turn-down-staff-reim';
import CancelStaffReim from './cancel-staff-reim';

import * as orderSer from '@/api/services/order.service';
import * as logisticsSer from "@/api/services/logistics.service";
import * as commonSer from "@/api/services/common.service";

export default {
  name: 'DriverItem',
  mixins: [DataGridBase, DataGridResize],
  components: {
    TableSelfSet,
    Pagination,
    AdupDriverStaffReim,
    AuditStaffReim,
    TurnDownStaffReim,
    CancelStaffReim,
  },
  data() {
    let cols = [
      // {label: '标示号', field: 'unitNum', width: 80},
      { label: '收款人', field: 'reimUser.realName', width: 80 },
      { label: '经办人', field: 'operUser.realName', width: 80 },
      { label: '报账类型', field: 'reimTypeId.typeName', width: 100 },
      { label: '报账状态', field: 'isCheck', width: 80 },
      { label: '添加日期', field: 'addTime', width: 130 },
      { label: '报账部门', field: 'deptId.name', width: 80 },
      { label: '驳回原因', field: 'refuseReason', width: 200 },
      { label: '图片', field: 'reimVoucherUrl', width: 100 },
      { label: '车牌号', field: 'plateNum', width: 80 },
      { label: '收入', field: 'gathMoney', width: 60 },
      { label: '支出', field: 'payMoney', width: 60 },

      { label: '订单号', field: 'orderNum', width: 100 },
      { label: '行程详情', field: 'routeDetail', width: 300 },
      { label: '行程开始日期', field: 'routeSdate', width: 90 },
      { label: '行程结束日期', field: 'routeEdate', width: 90 },
      { label: '是否高速', field: 'isDeep', width: 70 },

      { label: '费用产生开始日期', field: 'costStartDate', width: 120 },
      { label: '费用产生开始时间', field: 'startTime', width: 120 },
      { label: '费用产生结束日期', field: 'costEndDate', width: 120 },
      { label: '费用产生结束时间', field: 'endTime', width: 120 },
      { label: '行程备注', field: 'routeRemark', width: 150 },
      { label: '车辆公里数', field: 'currentKilo', width: 100 },

      { label: '打单费', field: 'singleFee', width: 100 },
      { label: '洗车费', field: 'washingFee', width: 100 },
      { label: '停车费', field: 'parkingFee', width: 100 },
      { label: '过路费', field: 'roadFee', width: 100 },
      { label: '用餐费', field: 'livingFee', width: 100 },
      { label: '买水费', field: 'waterFee', width: 100 },
      { label: '住宿费', field: 'stayFee', width: 100 },
      { label: '其他费', field: 'otherFee', width: 100 },

      { label: '操作标识', field: 'operMark', width: 100 },
    ];

    return {
      // 文件地址前缀
      fileBaseUrl: this.C.fileBaseUrl,
      // 数据列表参数
      dg: {
        dgName: 'dg',
        tbName: 'driverItemTb',
        total: 0,
        list: [],
        srcCols: cols,
        columns: [],
        selRows: [],
        ps: {
          page: 1, rows: 20,
          // 0办公室报账 1驾驶员报账
          staffReimWay: '1',
          // 搜索时间类型
          timeType: '1',
          // 查询起止日期
          sedate: '',
          // 开始日期
          startTime: "",
          // 结束日期 
          endTime: "",
          // 报账状态 默认未审核
          isCheck: ['NO_CHECK', 'CHECKED'],
          // 驾驶员付款类型
          driverPayType: '',
          // 经办人
          uname: '',
          // 收款人
          cusUname: '',
          // 报账金额 
          money: '',
          // 车牌号
          plateNum: '',
          // 报账类型
          reimTypeId: '',
          // 操作标识  
          operMark: '',
          // 子订单号
          orderNum: '',
          // 摘要 
          remark: '',
        },

        // 累计-记账收入
        totalGath: 0,
        // 累计-记账支出 
        totalPay: 0,
        // 累计-打单费
        singleFee: 0,
        // 累计-洗车费
        washingFee: 0,
        // 累计-停车费
        parkingFee: 0,
        // 累计-过路费
        roadFee: 0,
        // 累计-用餐费
        livingFee: 0,
        // 累计-买水费
        waterFee: 0,
        // 累计-住宿费
        stayFee: 0,
        // 累计-其他费
        otherFee: 0,

      },
      efficiency: 0,// 效率值
      // 添加/修改-驾驶员报账-弹框
      adupDriverStaffReimMd: { state: false },
      // 修改-驾驶员报账-弹框
      updDriverStaffReimMd: { state: false },
      // 审核报账-弹框
      auditStaffReimMd: { state: false },
      // 报账驳回-弹框
      turnDownStaffReimMd: { state: false },
      // 报账核销-弹框
      cancelStaffReimMd: { state: false },

      // 部门-选择列表
      departmentOptions: [],
      // 报账类型-选择列表 
      reimTypeOptions: [],
      // 车牌号-选择列表
      carNumList: [],
      // 经办人-选择列表
      serviceManList: [],
      // 收款人-选择列表
      cusUnameOptions: [],
      // // 驾驶员记账类型-选择列表
      // driverOptions: [
      //   {label: '记账维修', value: '0'},
      //   {label: '现金维修', value: '1'},
      //   {label: '现金加油', value: 'XJ_JY'},
      //   {label: '油票加油', value: 'YP_JY'},
      //   {label: '油罐车加油', value: 'YGC_JY'},
      //   {label: '充值卡加油', value: 'CZK_JY'},
      // ],

    }
  },
  computed: {
    // 所选记账-收入
    seledGath() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { money += parseFloat(it.gathMoney); });
      return (money).toFixed(2);
    },
    // 所选记账-支出
    seledPay() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { money += parseFloat(it.payMoney); });
      return (money).toFixed(2);
    },
    // 所选记账-打单费
    seledSingleFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.singleFee)); });
      return (money).toFixed(2);
    },
    // 所选记账-洗车费
    seledWashingFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.washingFee)); });
      return (money).toFixed(2);
    },
    // 所选记账-停车费
    seledParkingFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.parkingFee)); });
      return (money).toFixed(2);
    },
    // 所选记账-过路费
    seledRoadFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.roadFee)); });
      return (money).toFixed(2);
    },
    // 所选记账-用餐费
    seledLivingFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.livingFee)); });
      return (money).toFixed(2);
    },
    // 所选记账-买水费
    seledWaterFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.waterFee)); });
      return (money).toFixed(2);
    },
    // 所选记账-住宿费
    seledStayFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.stayFee)); });
      return (money).toFixed(2);
    },
    // 所选记账-其他费
    seledOtherFee() {
      let money = 0, rows = this.getSeledRows();
      rows.forEach(it => { it.orderTrade && (money += parseFloat(it.orderTrade.otherFee)); });
      return (money).toFixed(2);
    },
    // 效率值
    seledOtherFee1() {
      let efficiency = this.efficiency
      return efficiency
    },

  },
  created() {
    const that = this;

    that.initTbSetData(that.dg.dgName);

    that.getDgList();
    that.init();
  },
  methods: {
    init() {
      // 获取-部门数据
      commonSer.getFindDepts({ page: 1, rows: 9999 }).then(res => {
        this.departmentOptions = res.data
      });

      // 报账类型数据
      commonSer.getReimTypeDict({ addWay: 1, type: 0 }).then(res => {
        this.reimTypeOptions = res.data
      });

      // 车牌号
      logisticsSer.getAllPlateNum({ status: 0 }).then(res => {
        this.carNumList = Object.keys(res.plateNums).map((item) => ({
          label: item,
          value: item,
        }));
      });

      // 员工下拉(经办人)
      commonSer.getStaffCombo({ staffState: "NORMAL,TRY" }).then((res) => {
        this.serviceManList = res.data || [];
      });
    },
    // 查询-收款人-列表
    getDriverList(query) {
      if (!query) {
        this.cusUnameOptions = [];
        return;
      }
      commonSer.getDriverDict({ find: query }).then(res => {
        this.cusUnameOptions = res.data;
      });
    },

    // 获取表格数据
    getDgList() {
      let that = this, U = that.U, ps = U.scopy(that.dg.ps);

      // 起止日期
      ps.sTime = (ps.sedate && ps.sedate.length === 2) ? ps.sedate[0] : '';
      ps.eTime = (ps.sedate && ps.sedate.length === 2) ? ps.sedate[1] : '';
      delete ps.sedate;
      // 报账类型
      ps.reimTypeId = (ps.reimTypeId && ps.reimTypeId.length > 0) ? ps.reimTypeId.join(',') : '';
      // 审核状态
      ps.isCheck = (ps.isCheck && ps.isCheck.length > 0) ? ps.isCheck.join(',') : '';

      orderSer.getStaffReimList(ps).then(res => {
        if (res.code !== 1) {
          U.qerror(res.msg);
        } else {
          let list = res.data;
          that.efficiency = res.efficiency
          list.forEach(it => { it.dgName = that.dg.dgName; });
          that.dg.list = list;
          that.dg.total = res.count;

          // 累计收入
          that.dg.totalGath = parseFloat(res.totalGath);
          // 累计支出
          that.dg.totalPay = parseFloat(res.totalPay);
          // 累计-打单费
          that.dg.singleFee = parseFloat(res.singleFee);
          // 累计-洗车费
          that.dg.washingFee = parseFloat(res.washingFee);
          // 累计-停车费
          that.dg.parkingFee = parseFloat(res.parkingFee);
          // 累计-过路费
          that.dg.roadFee = parseFloat(res.roadFee);
          // 累计-用餐费
          that.dg.livingFee = parseFloat(res.livingFee);
          // 累计-买水费
          that.dg.waterFee = parseFloat(res.waterFee);
          // 累计-住宿费
          that.dg.stayFee = parseFloat(res.stayFee);
          // 累计-其他费
          that.dg.otherFee = parseFloat(res.otherFee);
        }
      });
    },

    // 显示/隐藏-添加/修改驾驶员报账-弹框
    setAdupDriverStaffReimMd(e) {
      const that = this, U = that.U, md = that.adupDriverStaffReimMd, rows = that.getSeledRows();

      if (!e.state) {
        md.obj = null;
        md.state = false;
      } else {
        if (e.type == 1) {// 新增
          md.obj = null;
          md.state = true;
        } else {// 修改
          if (rows.length == 0) {
            U.qerror("请选择一条报账记录进行修改");
          } else if (rows.length > 1) {
            U.qerror("只能选择一条报账记录进行修改");
          } else {
            let row = rows[0];
            if (row.isCheck != 'NO_CHECK') {
              U.qerror("所选报账记录【未审核】，无法修改");
            } else {
              md.obj = row;
              md.state = true;
            }
          }
        }
      }
    },

    // 审核报账
    auditStaffReim(isCheck) {
      const that = this, U = that.U, rows = that.getSeledRows();

      if (rows.length === 0) {
        U.qerror("请至少选择一条数据进行审核");
      } else {
        let fg = true;

        if (fg) {
          let isObj = rows.find(it => it.isCheck != 'NO_CHECK');
          if (isObj) fg = U.qerror(`所选第 ${isObj.index + 1} 行报账存在【已审核】记录，不能操作`);
        }

        if (isCheck === 'CHECKED') {
          if (fg) {
            let message = '是否将选中报账<b class="c-red">通过审核</b>？';
            U.qconfim(message, '审核').then(r => {
              if (r) {
                let idArr = rows.map(item => item.id);
                let ps = { ids: idArr.join(','), alNoticeChoice: '', note: '', isCheck };
                orderSer.checkStaffReimburse(ps).then(res => {
                  if (res.code !== 1) {
                    U.qerror(res.msg);
                  } else {
                    U.qsuccess(res.msg);

                    that.getDgList();
                  }
                })
              }
            });
          }
        } else if (isCheck === 'REFUSED') {
          if (fg) {
            let message = '请输入审核<b class="c-red">不通过原由</b>';
            U.qprompt(message, '审核').then(r => {
              if (r?.action == 'confirm' && !r?.value) {
                U.qerror('审核不通过原由不能为空！');
              } else {
                let idArr = rows.map(item => item.id);
                let ps = { ids: idArr.join(','), alNoticeChoice: '', note: r.value, isCheck };
                orderSer.checkStaffReimburse(ps).then(res => {
                  if (res.code !== 1) {
                    U.qerror(res.msg);
                  } else {
                    U.qsuccess(res.msg);

                    that.getDgList();
                  }
                })
              }
            });
          }
        }

      }
    },
    // 驳回报账
    setTurnDownStaffReimMd(e) {
      const that = this, U = that.U, rows = that.getSeledRows(), md = that.turnDownStaffReimMd;

      if (!e.state) {
        md.list = null;
        md.state = false;
      } else {
        if (rows.length === 0) {
          U.qerror("请至少选择一条数据进行驳回");
        } else {
          // 只有已审核的才能驳回
          let isCheck = rows.every(it => it.isCheck === 'CHECKED');
          if (!isCheck) {
            U.qerror("只有【已审核】的才能进行驳回操作");
          } else {
            md.list = rows;
            md.state = true;
          }
        }
      }
    },
    // 核销-弹框
    setCancelStaffReimMd(e) {
      const that = this, U = that.U, rows = that.getSeledRows(), md = that.cancelStaffReimMd;

      if (!e.state) {
        md.list = null;
        md.state = false;
      } else {
        if (rows.length === 0) {
          U.qerror("请至少选择一条数据进行核销");
        } else {
          // 只有已审核的才能核销
          let isCheck = rows.every(it => it.isCheck === 'CHECKED');
          if (!isCheck) {
            U.qerror("只有【已审核】的才能进行核销操作");
          } else {
            let retArr = rows.map(it => (it.reimUser && it.reimUser.uname));
            let isEqualUser = [...new Set(retArr)]
            if (isEqualUser && isEqualUser.length > 1) {
              U.qerror("核销收款人必须是同一用户");
            } else {
              md.list = rows;
              md.state = true;
            }
          }
        }
      }
    },

    // 撤销核销
    cancelWriteOff() {
      const that = this, U = that.U, rows = that.getSeledRows();

      if (rows.length == 0) {
        U.qerror("请至少选择一条要撤销的数据");
      } else {
        let isObj = rows.find(it => it.isCheck != 'CANCELLATION');
        if (isObj) {
          U.qerror(`所选第 ${isObj.index + 1} 行数据不是【已报销】，不能撤销`);
        } else {
          U.qconfim(`确定<span class="c-red">撤销 ${rows.length} 条</span>【已报销】的报账记录？`).then(r => {
            if (r) {
              let srIds = rows.map(it => it.id).join(',');
              orderSer.cancelWriteOff({ srIds: srIds }).then(res => {
                if (res.code != 1) {
                  U.qerror(res.msg);
                } else {
                  U.qsuccess(res.msg);

                  that.getDgList();
                }
              });
            }
          });
        }
      }
    },

    // 删除报账
    delStaffReim() {
      let that = this, U = that.U, rows = that.dg.selRows;

      if (!rows.length) return U.qerror("请勾选要删除的信息");

      U.qconfim(`确定<span class="c-red">删除所选报账</span>？`).then(r => {
        if (r) {
          let delIds = rows.map(item => item.id);
          orderSer.delStaffReim({ delIds: delIds.join(',') }).then(res => {
            if (res.code != 1) {
              U.qerror(res.msg);
            } else {
              U.qsuccess(res.msg);

              that.getDgList();
            }
          });
        }
      });
    },

    // 导出-报账记录
    exportStaffReim() {
      let that = this, U = that.U, ps = U.scopy(that.dg.ps);

      // 起止日期
      ps.sTime = (ps.sedate && ps.sedate.length === 2) ? ps.sedate[0] : '';
      ps.eTime = (ps.sedate && ps.sedate.length === 2) ? ps.sedate[1] : '';
      delete ps.sedate;
      // 报账类型
      ps.reimTypeId = (ps.reimTypeId && ps.reimTypeId.length > 0) ? ps.reimTypeId.join(',') : '';
      // 审核状态
      ps.isCheck = (ps.isCheck && ps.isCheck.length > 0) ? ps.isCheck.join(',') : '';

      ps.rows = that.dg.total;
      orderSer.staffReimExport(ps).then(res => {
        if (res.code !== 1) {
          U.qerror(res.msg);
        } else {
          exportExcel({
            filename: '驾驶员报账',
            data: res.data,
            enumData: {
              "operNote": "操作备注",
              "gathMoney": "收入金额",
              "addTime": "添加时间",
              "reimTypeId": "报账类型",
              "deptId": "操作部门",
              "unitNum": "单位编号",
              "remark": "备注",
              "reqsrc": "报账来源",
              "plateNum": "车牌号0",
              "payMoneyDetail": "支出详情",
              "costEndDate": "费用产生结束日期",
              "reimUser": "收款人",
              "isCheck": "报销状态",
              "payMoney": "支出金额",
              "costStartDate": "费用产生开始日期",
              "reimUnitName": "收款人单位名称",
              "operMark": "操作编号",
              "operUser": "经办人",
              "refuseReason": "驳回原因",
              "enterTime": "入账时间",
              "payDate": "支付日期"
            }
          });
        }
      })
    }
  }
}
</script>
